package util
{
	import flash.events.IOErrorEvent;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayList;
	import mx.controls.AdvancedDataGrid;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridColumnGroup;
	
	import spark.components.DataGrid;
	import spark.components.List;
	import spark.components.gridClasses.GridColumn;

	public class Dg2Excel
	{
		import com.as3xls.xls.ExcelFile;
		import com.as3xls.xls.Sheet;			
		import mx.collections.ArrayCollection;   
		import mx.controls.dataGridClasses.DataGridColumn; 
		import com.as3xls.xls.Cell; 
		import mx.collections.ArrayCollection; 
		import com.as3xls.xls.Sheet; 
		import com.as3xls.xls.ExcelFile; 
		
		private static var fileReference:FileReference; 
		private static var xls:Class; 
		private  static var sheet:Sheet; 
		[Bindable] 
		private static var fields:Array; 
		
		public function Dg2Excel()
		{
		}

		public static function exportDgToExcel(dg:DataGrid):void {
			sheet = new Sheet();
			fields= new Array()
			var arr:ArrayCollection =dg.dataProvider as ArrayCollection; 
			
			sheet.resize(arr.length + 1,dg.columnsLength); 
			var columns:ArrayList =dg.columns as ArrayList;
		
			var i:int = 0; 
			for each (var field:GridColumn in columns.source){ 
				fields.push(field.dataField.toString());/*  .toString() */
				sheet.setCell(0,i,field.headerText.toString()); 
				i++; 
			}  
			var colCount:int = dg.columnsLength; 
			for(var r:int=0; r<arr.length; r++)    {     
				var record:Object=arr.getItemAt(r);     
				insertRecordInSheet(colCount,r+1,sheet,record); 
			}
			save();
		}
	
		public static function exportAdVanceGdToExcel(dg:AdvancedDataGrid):void 
		{ 
			sheet = new Sheet();
			fields= new Array()
			var arr:ArrayCollection =dg.dataProvider as ArrayCollection; 
			
			sheet.resize(arr.length + 1,dg.columnCount); 
			var columns:Array = dg.columns;
			var i:int = 0; 
		//	dg.groupedColumns[i].hasOwnProperty("children")
				
			for(var i:int = 0;i<dg.groupedColumns.length;i++) {
				var field:AdvancedDataGridColumn=dg.groupedColumns[i];
				if(field.hasOwnProperty("children")){
					
				}
				else
				{
					fields.push(field.dataField.toString());/*  .toString() */
					sheet.setCell(0,i,field.headerText.toString()); 
					i++; 
				}
			}
				
			for each(var field:AdvancedDataGridColumn in dg.groupedColumns){
				if(field is AdvancedDataGridColumnGroup){
					sheet.setCell(0,i,field.headerText.toString()); 
					for each(var field2:AdvancedDataGridColumn in (field as AdvancedDataGridColumnGroup).children){
						fields.push(field2.dataField.toString());/*  .toString() */
						sheet.setCell(1,i,field2.headerText.toString()); 
						
						i++; 
					}
				}
				else{
					fields.push(field.dataField.toString());/*  .toString() */
					sheet.setCell(0,i,field.headerText.toString()); 
					i++; 
				}
			}
//			for each (var field:AdvancedDataGridColumn in columns){ 
//				fields.push(field.dataField.toString());/*  .toString() */
//				sheet.setCell(0,i,field.headerText.toString()); 
//				i++; 
//			}  
			var colCount:int = dg.columnCount; 
			for(var r:int=0; r<arr.length; r++)    {     
				var record:Object=arr.getItemAt(r);     
				insertRecordInSheet(colCount,r+2,sheet,record); 
			}
			save();	
		} 
		private static function save():void{
			var xls:ExcelFile = new ExcelFile(); 
			xls.sheets.addItem(sheet); 
			var bytes: ByteArray = xls.saveToByteArray(); 
			var fr:FileReference = new FileReference(); 
			fr.save(bytes,"test.xls");  
			fr.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);	
		}
		private static function   errorHandler(event:IOErrorEvent):void{
			mx.controls.Alert.show("无法导出，同一目录下的同名文件正在被另一个人或程序使用，关闭其同名软件，或重命名，或选择其他目录保存","导出execl出错");
		}
		private static function insertRecordInSheet(colCount:int,row:int,shet:Sheet,recod:Object):void 
		{ 
			for(var c:int=0; c < colCount; c++) 
			{ 
				var i:int = 0; 
				for each(var field:String in fields){ 
					for each (var value:String in recod){ 
						if (recod[field]==null)
							recod[field]="";
						if (recod[field].toString() == value) 
							shet.setCell(row,i,value); 
					} 
					i++; 
				} 
			} 
		} 
	}
}